package cn.airfei.netty.nio.groupChat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

/**
 * @description:
 * @author: air
 * @create: 2021-01-15 10:16
 */
public class GroupChatClient {
    private final String HOST="127.0.0.1";
    private final int PORT=6667;
    private Selector selector;
    private SocketChannel socketChannel;

    private String username;

    public GroupChatClient() throws IOException {
       selector = Selector.open();
       socketChannel=SocketChannel.open(new InetSocketAddress(HOST,PORT));
       // 设置为非阻塞
       socketChannel.configureBlocking(false);
       socketChannel.register(selector, SelectionKey.OP_READ);
       username=socketChannel.getLocalAddress().toString().substring(1);
        System.out.println(username+" is ok");

    }

    // 向服务器发送消息
    public void sendInfo(String info){
        info=username+ " 说: "+info;

        try {
            socketChannel.write(ByteBuffer.wrap(info.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readInfo(){
        try {
            int readChannels = selector.select(1000);
            if (readChannels>0){
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while ( iterator.hasNext()){
                    SelectionKey key = iterator.next();
                    if (key.isReadable()){
                        SocketChannel channel = (SocketChannel) key.channel();
                        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
                        channel.read(byteBuffer);
                        // 把读到缓冲区的数据转成字符串
                        String msg = new String(byteBuffer.array());
                        System.out.println(msg.trim());
                    }
                }
                // 删除当前SelectionKey 防止重复操作
                iterator.remove();
            }else {
//                System.out.println("没有可以用的通道...");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        GroupChatClient groupChatClient = new GroupChatClient();
        // 启动一个线程
        new Thread(){
            @Override
            public void run() {
                while (true){
                    groupChatClient.readInfo();
                    try {
                        Thread.currentThread().sleep(2000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        // 发送数据给服务器
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            String s = scanner.nextLine();
            groupChatClient.sendInfo(s);
        }


    }

}
